package orest.front.achieve;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orest.back.userneeds.UserNeedsHandler;
import orest.back.userneeds.UserNeedsSql;

import net.ssta.common.Constants;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.HandlerBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;
import net.ssta.core.util.DateUtility;


/**
 * 
 * @ClassName: IllegalInfoRecordHandler
 * @Description: 信息推荐管理
 * @author:
 * @date:
 * 
 */
public class AchieveHandler extends HandlerBase
{
    
	 /**
     * 测试动态表格
     */
    public RecordSet testFillTable()
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = "SELECT * FROM T_UNIT";
            recordSet = m_executor.find(sql);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    /**
     * 根据ID获得成果信息
     */
    public static Record getKjcgById(String kjcgid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = new Record();
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            recordSet = m_executor.find(AchieveSql.getKjcgById(kjcgid));
            if(recordSet!=null&&recordSet.size()>0)
            	record = recordSet.get(0);
            return record;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    /**
     * 科技成果列表
     */
    public RecordSet getKjcgList(RequestKit requestKit)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            recordSet = m_executor.find(AchieveSql.getKjcgList(requestKit));
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 获取子分类列表
     */
    public RecordSet getSubFlByFatherOptValue(RequestKit requestKit)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
        	String tablename=requestKit.getParameter("tablename","");
        	String fieldname=requestKit.getParameter("fieldname","");
        	String fatheroptvalue=requestKit.getParameter("fatheroptvalue","");
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            recordSet = m_executor.find(AchieveSql.getSubFlByFatherOptValue(tablename,fieldname,fatheroptvalue));
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    /**
     * 获得推荐科技成果列表
     */
    public RecordSet getRecommendKjcgList(RequestKit requestKit)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            recordSet = m_executor.find(AchieveSql.getRecommendKjcgList(requestKit));
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    
    /**
     * 获得新能源分类,分类可能是2级的,每个Record表示一个分类,如果record的CHILDLIST不为空,表示该分类有子分类，了分类也是一个RecordSet
     */
    public static RecordSet getNewEnergyClassList()
    {
        Connection conn = null;
        RecordSet recordSet = null;
        RecordSet newEnergyClassRecordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            recordSet = m_executor.find(AchieveSql.getNewEnergyClassList());
            
            newEnergyClassRecordSet  = new RecordSet();
            for(int i=0;i<recordSet.size();i++){
            	Record record = recordSet.get(i);
            	String optionvalue = record.getString("OPTIONVALUE", "");
            		if(newEnergyClassRecordSet!=null&&newEnergyClassRecordSet.size()>0){
            			//前一个分类
            			Record pre_record = (Record)newEnergyClassRecordSet.get(newEnergyClassRecordSet.size()-1);
            			
            			//如果newEnergyClassList中的最后一个是当前分类的父分类
            			String preOptValue = pre_record.getString("OPTIONVALUE","");
            			if(optionvalue.startsWith(preOptValue)&&preOptValue.indexOf(".")==-1){
            				
            				if(pre_record.getObjectValue("CHILDLIST")!=null){
            					((RecordSet)(pre_record.getObjectValue("CHILDLIST"))).add(record);
            				}else{
            					RecordSet rst = new RecordSet();
            					rst.add(record);
            					(pre_record).addData("CHILDLIST",rst);
            				}
            			}else{
            				newEnergyClassRecordSet.add(record);
            			}
            		}else{
            			newEnergyClassRecordSet.add(record);
            		}
            }
            return newEnergyClassRecordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
}
